package com.admin.library.service.impl;

import com.admin.library.domain.entity.Publisher;
import com.admin.library.domain.entity.Reader;
import com.admin.library.mapper.PublisherMapper;
import com.admin.library.service.PublisherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Lii
 * @create 2022-05-26 20:38
 */

@Service("publisherService")
public class PublisherServiceImpl extends ServiceImpl<PublisherMapper, Publisher> implements PublisherService {

    @Resource
    private PublisherService publisherService;

    @Autowired
    private  PublisherMapper publisherMapper;


    @Override
    public Map<String, Object> page(Page<Publisher> page, Publisher publisherer) {

        String publisherId = publisherer.getPublisherId();
        String publisher = publisherer.getPublisher();
        String publisherLocation = publisherer.getPublisherLocation();

        QueryWrapper<Publisher> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(!StringUtils.isEmpty(publisher), Publisher::getPublisherId, publisherId)
                .like(!StringUtils.isEmpty(publisher), Publisher::getPublisher, publisher)
                .like(!StringUtils.isEmpty(publisherLocation), Publisher::getPublisherLocation, publisherLocation);


        Page<Publisher> selectPage = this.baseMapper.selectPage(page, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("list", selectPage.getRecords());
        map.put("total", selectPage.getTotal());
        return map;
    }

//
//    @Override
    public IPage<Publisher> getPage(int currentPage, int pageSize, Publisher publisherer) {
        LambdaQueryWrapper<Publisher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Strings.isNotEmpty(publisherer.getPublisherId()),Publisher::getPublisherId,publisherer.getPublisher());
        lambdaQueryWrapper.like(Strings.isNotEmpty(publisherer.getPublisher()),Publisher::getPublisher,publisherer.getPublisher());
        lambdaQueryWrapper.like(Strings.isNotEmpty(publisherer.getPublisherLocation()),Publisher::getPublisherLocation,publisherer.getPublisherLocation());
        IPage page = new Page(currentPage,pageSize);
        publisherMapper.selectPage(page,lambdaQueryWrapper);
        return page;
    }
    }
